/**
 * 角色管理的单例
 */
var Menu = {
    table: {},
    layerIndex: -1
};

Menu.init = function () {
    layui.form.render();
    var me = this;
    this.table = new LayUITreeGrid("menuTable", "/system/sysMenu/list", this.initColumns());
    me.table.params = {
        height: $(".dawn-content").height() - 50
    }
    me.table.setQueryParams(me.queryParams);
    me.table.handleListenToolBar = function (event, checkStatus, obj) {
        switch (event) {
            case "add":
                layui.laytpl($("#info").html()).render({}, function (html) {
                    Menu.itemIndex = layer.open({
                        type: 1,
                        offset: 'r',
                        anim: 'slideLeft', // 从右往左
                        shade: 0.1,
                        shadeClose: true,
                        title: '添加菜单',
                        area: ['800px', '100%'], //宽高
                        content: html,
                        success: function () {
                            Menu.add();
                        }
                    });
                });
                break;
        }
    };
    me.table.handleListenTool = function (event, data, obj) {
        switch (obj.event) {
            case "edit":
                layui.laytpl($("#info").html()).render({}, function (html) {
                    Menu.itemIndex = layer.open({
                        type: 1,
                        offset: 'r',
                        anim: 'slideLeft', // 从右往左
                        shade: 0.1,
                        shadeClose: true,
                        title: '修改菜单',
                        area: ['800px', '100%'], //宽高
                        content: html,
                        success: function () {
                            Menu.update(data.id);
                        }
                    });
                });
                break;
            case "delete":
                var operation = function () {
                    $request.get("/system/sysMenu/remove", function (data) {
                        if (data.success) {
                            Dawn.success("删除成功!");
                            Menu.table.refresh();
                        } else {
                            Dawn.error(data.message);
                        }
                    }).set("menuId", obj.data.id).start();
                };
                Dawn.deleteConfirm("是否刪除该菜单?", operation);
                break;
        }
    };
    me.table.init();
    Menu.addEvents();
    layui.form.render();
};

Menu.update = function(id) {
    $request.get("/system/sysMenu/info", function(data) {
        if(data.success) {
            layui.form.val("info", data.data);
            layui.form.render();
            $("#pcodeName").val(data.data.pcodeName == '' ? "顶级" : data.data.pcodeName);
            Menu.initZTree();
            layui.form.on('submit(save)', function (data) {
                $request.postJson("/system/sysMenu/edit", function (res) {
                    if (res.success) {
                        Dawn.success("更新成功!");
                        Menu.table.refresh();
                        layui.layer.close(Menu.itemIndex);
                    } else {
                        Dawn.error(res.message);
                    }
                }).setData(data.field).start();
                return false;
            });
        } else {
            Dawn.error(data.message);
        }
    }).setData({
        menuId: id
    }).setAsync(false).start();
};

Menu.add = function() {
    layui.form.render();
    Menu.initZTree();
    layui.form.on('submit(save)', function (data) {
        $request.postJson("/system/sysMenu/add", function (res) {
            if (res.success) {
                Dawn.success("保存成功!");
                Menu.table.refresh();
                layui.layer.close(Menu.itemIndex);
            } else {
                Dawn.error(res.message);
            }
        }).setData(data.field).start();
        return false;
    });
};

Menu.initZTree = function() {
    var ztree = new $ZTree("pcodeTree", "/system/sysMenu/selectMenuTreeList");
    ztree.bindOnClick(Menu.onClickDept);
    ztree.init();
    $("#pcodeName").unbind();
    $("#pcodeName").click(function () {
        if($("#pcodeTreeDiv").is(":hidden")) {
            Dawn.showInputTree("pcodeTreeDiv");
        } else {
            Dawn.hideInputTree("pcodeTreeDiv");
        }

    });
};

Menu.onClickDept = function(e, treeId, treeNode) {
    $("#pcodeName").val(treeNode.name);
    $("input[name='pcode']").val(treeNode.id);
    Dawn.hideInputTree("pcodeTreeDiv");
};



Menu.addEvents = function () {
    var me = this;
    $("#search").click(function () {
        me.search();
    })
    $("#reset").click(function () {
        me.reset();
    });
}

Menu.reset = function () {
    $("#menuName").val("");
    $("#level").val("");
    layui.form.render();
    this.search();
}

Menu.search = function () {
    Menu.init();
}


Menu.queryParams = function () {
    var queryData = {};
    queryData['menuName'] = $("#menuName").val();
    queryData['level'] = $('#level').val();
    return queryData;
}

Menu.initColumns = function () {
    return [[
        {
            field: 'name',
            title: '菜单名称',
            align: 'left'
        },
        {
            field: 'code',
            title: '菜单编号',
            align: 'left'
        },
        {
            field: 'url',
            title: '请求地址',
            align: 'left'
        },
        {
            field: 'num',
            title: ' 层级',
            align: 'center',
            width: 60
        },
        {
            field: 'isMenuName',
            title: '菜单',
            align: 'center',
            width: 60
        }, {
            fixed: 'right',
            title: '操作',
            toolbar: '#menuTableTool',
            align: 'center',
            width: 120
        }
    ]];
}


$(function () {
    layui.element.init();
    Menu.init();
});

